<?php
//error_reporting(0);
//include("../settings.php")

include("settings.php");

class MySQL extends CONFIGURE {
		
	function __construct()
	{
		$db = mysql_connect($this->db_host,$this->db_user,$this->db_password) or die(mysql_error());
		mysql_select_db($this->db_name, $db) or die(mysql_error());
		
		/*$this->admin_mail = $this->getAdminMail();*/
		$config_var = $this->getSiteStatus();
		$this->online_status = $config_var['live_status']; 
		$this->offline_message = $config_var['offline_message']; 
		$this->site_title = $config_var['site_title']; 
		$this->site_url = $config_var['site_url'];
	}
	
	function getSiteStatus() {
		$sql = "SELECT live_status,offline_message,	site_url, site_logo 
				FROM ".$this->SITE_SETTINGS;				
		
		$res = $this->exec_query($sql);
		$data = $this->fetch_row($res);
		
		return $data;
	}
	
	function getConfigureVariables()
	{
		$sql = "SELECT `site_title`, `site_url`, `pagin_length` FROM ".$this->CONFIGURE;				
		
		$res = $this->exec_query($sql);
		$data = $this->fetch_row($res);
		
		return $data;
	}
	
	function getAdminMail() {
		$sql = "SELECT email FROM ".$this->ADMIN;				
		
		$res = $this->exec_query($sql);
		$data = $this->fetch_row($res);
		
		return $data['email'];
	}	
	
	public function exec_query($sql)
	{
		$result = mysql_query($sql) or die(mysql_error());
		return $result;
	}
	public function fetch_row($res)
	{
		$result = mysql_fetch_array($res);
		return $result;
	}
	
	public function fetch_row_all($res)
	{
		while($result[] = mysql_fetch_array($res));
		unset($result[count($result)-1]);//unset last empty row	
		return $result;
	}
	
	public function fetch_last_insert_id()
	{
		return(mysql_insert_id());
	}
	
	public function insert_query($table,$array) 
	{
		foreach($array as $field_name => $field_value) {
			$fld_name_str .= $field_name .",";
			$fld_value_str .= "'". mysql_escape_string($field_value) ."',";
		}
		
		$fld_name_str = substr($fld_name_str,0, (strlen($fld_name_str)-1));
		$fld_value_str = substr($fld_value_str,0, (strlen($fld_value_str)-1));
		$sql= "INSERT INTO ".$table."(".$fld_name_str.") values (".$fld_value_str.")";
		$ins_res = mysql_query($sql) or die(mysql_error());
		
		return $ins_res;
	}
	
	public function delete_query($table,$condition="") {
		$sql = "DELETE FROM ".$table;
		$sql = ($condition != "")?$sql.$condition:$sql;
		$result = mysql_query($sql) or die(mysql_error());
	}
	
	public function update_query($table, $array, $condition="") {
		$field_value_pair = "";
		foreach($array as $field_name => $field_value) {
			$field_value_pair .= $field_name."= '".mysql_escape_string($field_value)."',";
		}
		$field_value_pair = substr($field_value_pair,0, (strlen($field_value_pair)-1));
		
		$sql = "UPDATE ".$table." SET ".$field_value_pair.$condition;
		$result = mysql_query($sql) or die(mysql_error());
	}
}	
?>